<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="waName">
        <el-input
          v-model="queryParams.waName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工号" prop="jobNum">
        <el-input
          v-model="queryParams.jobNum"
          placeholder="请输入工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hr:account:add']"
        >读取员工信息</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hr:account:edit']"
        >薪酬设置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hr:account:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hr:account:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="accountList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司代码" align="center" prop="factoryCode" />
      <el-table-column label="公司名称" align="center" prop="factoryName" />
      <el-table-column label="姓名" align="center" prop="waName" />
      <el-table-column label="工号" align="center" prop="jobNum"  width="140"/>
      <el-table-column label="部门" align="center" prop="department" />
      <el-table-column label="分部" align="center" prop="division" />
      <el-table-column label="组别" align="center" prop="waGroup" />
      <el-table-column label="岗位" align="center" prop="post" />
      <el-table-column label="入职时间" align="center" prop="entryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工龄(年)" align="center" prop="workAge" />
      <el-table-column width="120" label="是否计算加班" align="center" prop="isJsjb" />
      <el-table-column label="岗位分类" align="center" prop="gwClassification" />
      <el-table-column label="计酬方式" align="center" prop="jcWay" />
      <el-table-column label="基本工资" align="center" prop="baseWages" />
      <el-table-column width="120" label="岗位工资" align="center" prop="zrgwWages" />
      <el-table-column label="绩效工资" align="center" prop="jxWages" />
      <el-table-column label="小时工资" align="center" prop="hourMoney" />
      <el-table-column label="周末加班工资" align="center" prop="zmjbMoney" />
      <el-table-column label="节假日加班工资" align="center" prop="holidayMoney" />

      <el-table-column label="交通补贴" align="center" prop="jtbt" />
      <el-table-column label="通讯补贴" align="center" prop="txbt" />
      <el-table-column label="全勤奖" align="center" prop="qqj" />
      <el-table-column label="学历补贴" align="center" prop="xlbt" />
<!--      <el-table-column label="星级奖励" align="center" prop="xjjl" />-->
<!--      <el-table-column width="120" label="高温/取暖补贴" align="center" prop="gwbt" />-->
<!--      <el-table-column label="特殊补贴" align="center" prop="specialBt" />-->
      <el-table-column label="餐补" align="center" prop="cb" />
      <el-table-column label="其他补贴" align="center" prop="otherBt" />
      <el-table-column label="保险类型" align="center" prop="bxType" />
      <el-table-column label="保险" align="center" prop="insurance" />
      <el-table-column width="120" label="公积金类型" align="center" prop="gjjType" />
      <el-table-column width="120" label="住房公积金" align="center" prop="zfgjj" />
      <el-table-column label="工资卡号" align="center" prop="waCardNum" />
      <el-table-column label="开户银行" align="center" prop="openBank" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="140">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hr:account:edit']"
          >薪酬设置</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hr:account:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改薪酬基数设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-row>
          <el-col :span="8">
            <el-form-item label="工号" prop="jobNum">
              <el-input v-model="form.jobNum" placeholder="请选择员工" readonly/>
            </el-form-item>

          </el-col>
          <el-col :span="8">
            <el-form-item label="姓名" prop="waName">
              <el-input v-model="form.waName" placeholder="" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="部门" prop="department">
              <el-input v-model="form.department" placeholder="" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="分部" prop="division">
              <el-input v-model="form.division" placeholder="" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="组别" prop="waGroup">
              <el-input v-model="form.waGroup" placeholder="" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="岗位" prop="post">
              <el-input v-model="form.post" placeholder="" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="入职时间" prop="entryTime">
              <el-input v-model="form.entryTime" placeholder="" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工龄" prop="workAge">
              <el-input v-model="form.workAge" placeholder="请输入工龄" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计酬方式" prop="jcWay">
              <el-select v-model="form.jcWay" placeholder="请选择">
                <el-option
                  v-for="item in jcWayOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="岗位分类" prop="gwClassification">
              <el-input v-model="form.gwClassification" placeholder="请输入岗位分类" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="基本工资" prop="baseWages">
              <el-input v-model="form.baseWages" placeholder="请输入基本工资" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="岗位工资" prop="zrgwWages">
              <el-input v-model="form.zrgwWages" placeholder="请输入岗位工资" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="绩效工资" prop="jxWages">
              <el-input v-model="form.jxWages" placeholder="请输入绩效工资" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="交通补贴" prop="jtbt">
              <el-input v-model="form.jtbt" placeholder="请输入交通补贴" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="通讯补贴" prop="txbt">
              <el-input v-model="form.txbt" placeholder="请输入通讯补贴" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="小时工资" prop="hourMoney">
              <el-input v-model="form.hourMoney" placeholder="请输入小时工资" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="周末加班工资" prop="zmjbMoney">
              <el-input v-model="form.zmjbMoney" placeholder="请输入周末加班工资" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="节假日加班工资" prop="holidayMoney">
              <el-input v-model="form.holidayMoney" placeholder="请输入节假日加班工资" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="全勤奖" prop="qqj">
              <el-input v-model="form.qqj" placeholder="请输入全勤奖" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学历补贴" prop="xlbt">
              <el-input v-model="form.xlbt" placeholder="请输入学历补贴" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="其他补贴" prop="otherBt">
              <el-input v-model="form.otherBt" placeholder="请输入其他补贴" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="餐补" prop="cb">
              <el-input v-model="form.cb" placeholder="请输入餐补" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="保险类型" prop="bxType">
              <el-select v-model="form.bxType" placeholder="请选择">
                <el-option
                  v-for="item in bxType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="保险" prop="insurance">
              <el-input v-model="form.insurance" placeholder="请输入保险" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="公积金类型" prop="gjjType">
              <el-select v-model="form.gjjType" placeholder="请选择">
                <el-option
                  v-for="item in bxType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="住房公积金" prop="zfgjj">
              <el-input v-model="form.zfgjj" placeholder="请输入住房公积金" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工资卡号" prop="waCardNum">
              <el-input v-model="form.waCardNum" placeholder="请输入工资卡号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="开户银行" prop="openBank">
              <el-input v-model="form.openBank" placeholder="请输入开户银行" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否计算加班" prop="isJsjb">
              <el-select v-model="form.isJsjb" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listAccount, getAccount, delAccount, addAccount, updateAccount, updateSync} from "@/api/hr/account";
  import {getToken} from "@/utils/auth";

  export default {
    name: "Account",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        options: [{
          value: '是',
          label: '是'
        },{
          value: '否',
          label: '否'
        }],
        jcWayOptions: [{
          value: '小时',
          label: '小时'
        },{
          value: '月度',
          label: '月度'
        }],
        bxType: [{
          value: '定额',
          label: '定额'
        },{
          value: '比例',
          label: '比例'
        }],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 薪酬基数设置表格数据
        accountList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          factoryCode: null,
          factoryName: null,
          waName: null,
          jobNum: null,
          department: null,
          division: null,
          waGroup: null,
          post: null,
          currentYm: null,
          actualDayNum: null,
          restDayNum: null,
          leaveDayNum: null,
          departureChaDayNum: null,
          ycqDayNum: null,
          jjryqHours: null,
          workOvertime: null,
          tbdxDayNum: null,
          dxwTime: null,
          jbType: null,
          isJsjb: null,
          qnType: null,
          entryTime: null,
          workAge: null,
          insurSituation: null,
          gwClassification: null,
          isFormal: null,
          baseWages: null,
          zrgwWages: null,
          jtbt: null,
          txbt: null,
          wagesTotal: null,
          yfBasicWages: null,
          yfGwzrWages: null,
          yfJbWages: null,
          yfJjWages: null,
          yfJtWages: null,
          yfTxWages: null,
          qqj: null,
          xlbt: null,
          xjjl: null,
          otherBt: null,
          gwbt: null,
          specialBt: null,
          ck: null,
          cb: null,
          gzf: null,
          yfWagesTotal: null,
          insurance: null,
          zfgjj: null,
          grsds: null,
          yff: null,
          gzfkk: null,
          otherKk: null,
          khfmj: null,
          reward: null,
          actualWagesTotal: null,
          waCardNum: null,
          openBank: null,
          userInfo: null
        },
        // 表单参数
        form: {},
        upload: {
          // 是否显示弹出层（物料导入）
          open: false,
          // 弹出层标题（物料导入）
          title: "导入数据",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/hr/account/importData"
        },
        // 表单校验
        rules: {
          baseWages: [
            { required: true, trigger: "blur", message: "请填写基本工资！" }
          ],
          jxWages: [
            { required: true, trigger: "blur", message: "请填写绩效工资！" }
          ],
          zrgwWages: [
            { required: true, trigger: "blur", message: "请填写岗位工资！" }
          ],
          jcWay: [
            { required: true, trigger: "blur", message: "请选择计酬方式！" }
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询薪酬基数设置列表 */
      getList() {
        this.loading = true;
        listAccount(this.queryParams).then(response => {
          console.log(response.rows,"response.rows")
          this.accountList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          factoryCode: null,
          factoryName: null,
          waName: null,
          jobNum: null,
          department: null,
          division: null,
          waGroup: null,
          post: null,
          currentYm: null,
          actualDayNum: null,
          restDayNum: null,
          leaveDayNum: null,
          departureChaDayNum: null,
          ycqDayNum: null,
          jjryqHours: null,
          workOvertime: null,
          tbdxDayNum: null,
          dxwTime: null,
          jbType: null,
          isJsjb: null,
          qnType: null,
          entryTime: null,
          workAge: null,
          insurSituation: null,
          gwClassification: null,
          isFormal: null,
          baseWages: null,
          zrgwWages: null,
          jtbt: null,
          txbt: null,
          wagesTotal: null,
          yfBasicWages: null,
          yfGwzrWages: null,
          yfJbWages: null,
          yfJjWages: null,
          yfJtWages: null,
          yfTxWages: null,
          qqj: null,
          xlbt: null,
          xjjl: null,
          otherBt: null,
          gwbt: null,
          specialBt: null,
          ck: null,
          cb: null,
          gzf: null,
          yfWagesTotal: null,
          insurance: null,
          zfgjj: null,
          grsds: null,
          yff: null,
          gzfkk: null,
          otherKk: null,
          khfmj: null,
          reward: null,
          actualWagesTotal: null,
          remark: null,
          waCardNum: null,
          openBank: null,
          userInfo: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        updateSync().then(response => {
          if(response.code==200){
            this.getList();
            this.$modal.msgSuccess("同步成功");
          }
          loading.close();
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getAccount(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改薪酬基数设置";
        });
      },
      /** 提交按钮 */
      submitForm() {
        if(this.form.jcWay=='小时'){
          if(!this.form.hourMoney){
            this.$modal.msgSuccess("计酬方式为小时,请输入小时工资！")
            return;
          }
        }
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateAccount(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addAccount(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除薪酬基数设置编号为"' + ids + '"的数据项？').then(function() {
          return delAccount(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('hr/account/export', {
          ...this.queryParams
        }, `account_${new Date().getTime()}.xlsx`)
      },
      handleImport() {
        this.upload.title = "物料导入";
        this.upload.open = true;
      },
      /** 下载模板操作 */
      importTemplate() {
        this.download('hr/account/importTemplate', {
        }, `薪酬基数模板.xlsx`)
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      }
    }
  };
</script>
